﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;
using System.Text;

public partial class User_UC2Gift : System.Web.UI.Page
{
    private string act = Utils.RQ_str("act");
    private int id = Utils.RQ_int("id");
    private int pageIndex = Utils.RQ_int("pi");
    private int pageSize = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        switch (act)
        {
            case "del":
                Delete();
                break;
        }

        if (!IsPostBack)
            bindData();
        bindContrl();
    }

    private void Delete()
    {
        int uid = Master.ua.UserID;
        string url = "UC2Gift.aspx?pi=" + pageIndex + "&pz=" + pageSize;
        string Msg = "";
        clsGiftProduct cls = new clsGiftProduct(id);
        if (cls.UserID==uid)
        {
            clsGiftProduct.Delete(id);
            Msg = "删除成功！";
        }
        else
        {
            Msg = "无法删除！";
        }
        Utils.WrAlertGo(Msg, url);
    }

    private void bindData()
    {
        int uid = Master.ua.UserID;
        List<clsGiftProduct> ls = clsGiftProduct.GetCollection(uid);
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ls;
        pds.PageSize = pageSize;
        pds.CurrentPageIndex = (pageIndex - 1 < 0) ? 0 : (pageIndex - 1);
        pds.AllowPaging = true;

        ltPage.Text = LvEasyPager.CreateUser("UC2Gift.aspx?", pageIndex, pageSize, pds.DataSourceCount);

        StringBuilder sbHtml = new StringBuilder();

        foreach (clsGiftProduct gp in pds)
        {
            sbHtml.AppendLine("<tr>");
            sbHtml.AppendLine("<td><input type=\"checkbox\" id=\"ids\" name=\"ids\" value=\"" + gp.GiftProductID + "\" /></td>");
            sbHtml.AppendLine(string.Format("<td><img alt='{0}' src='/{1}' width='60' height='60' /></td>", gp.GiftProductName, gp.GiftProductPic));
            sbHtml.AppendLine(string.Format("<td align=\"left\"><a href='/gift.aspx?id={1}' target='_blank'>{0}</a></td>", gp.GiftProductName,gp.GiftProductID));
            sbHtml.AppendLine(string.Format("<td>{0}</td>", gp.GiftProductAddTime.ToString("yyyy-MM-dd")));
            sbHtml.AppendLine(string.Format("<td>{0}<span class='protj'>推荐</span></td>", ""));
            sbHtml.AppendLine(string.Format("<td><a class=\"hand\" href=\"UC2GiftOperate.aspx?act=edit&id={0}\">[修改</a> <a class=\"hand\" href=\"UC2Gift.aspx?act=del&id={0}\" onclick=\"return confirm('删除后无法恢复。\\n\\n确实要删除吗？')\">删除]</a>", gp.GiftProductID));
            sbHtml.AppendLine("</tr>");
        }
        tbodyList.InnerHtml = sbHtml.ToString();
    }

    private void bindContrl()
    {

    }

    protected void lkbtnBatchDel_Click(object sender, EventArgs e)
    {
        int uid = Master.ua.UserID;
        List<clsGiftProduct> ls = getDelList();
        bool isErr = false;
        string Msg = "以下不可删除：\\n\\n";
        if (ls.Count > 0)
        {
            foreach (clsGiftProduct s in ls)
            {
                bool CanDel = s.UserID == uid;
                if (CanDel) //可删除
                {
                    clsGiftProduct.Delete(s);
                }
                else
                {
                    isErr = true;
                    Msg += "  " + s.GiftProductName;
                }
            }
        }
        else
        {
            isErr = true;
            Msg = "您未勾选！";
        }
        string url = "UC2Gift.aspx?pi=" + pageIndex + "&pz=" + pageSize;
        if (isErr)
        {
            Utils.WrAlertGo(Msg, url);
        }
        else
        {
            Msg = "删除成功！";
            Utils.WrAlertGo(Msg, url);
        }
    }

    private List<clsGiftProduct> getDelList()
    {
        string ids = Utils.RF_str("ids");
        List<clsGiftProduct> ls = new List<clsGiftProduct>();
        if (ids != "")
        {
            string[] arrID = ids.Split(',');
            int arrLen = arrID.Length;
            for (int i = 0; i < arrLen; i++)
            {
                int id = 0;
                if (!int.TryParse(arrID[i].Trim(), out id))
                    id = 0;
                if (id > 0)
                {
                    clsGiftProduct s = new clsGiftProduct(id);
                    if (s.GiftProductID > 0)
                        ls.Add(s);
                }
            }
        }
        return ls;
    }
}